<template>
  <div>
    <el-input
      class="label-search"
      :placeholder="$t('wiki.list.labelSearch.pla1')"
      clearable
      v-model="searchLabel">
    </el-input>

    <div class="hot-labels">
      <div
        class="hot-labels-item"
        :key="index"
        :class="{
          'is-active': isActive === index
        }"
        @click="labelClick(label, index)"
        v-for="(label, index) in hotLabels">
        {{ label }}
      </div>
    </div>
  </div>
</template>

<script>
/* api */
import { getHotLabel } from '@src/api/Repository.js'
/* mixin */
import FormMixin from '@src/component/form/mixin/form'

export default {
  name: 'label-search',
  mixins: [FormMixin],
  props: {
    value: {
      type: String,
      default: '',
    }
  },
  watch: {
    value(newValue) {
      this.searchLabel = newValue
      if (this.hotLabels[this.isActive] !== newValue) {
        this.isActive = -1
      }
    },
    searchLabel(newValue) {
      this.inputLabel(newValue)
      this.$emit('input', newValue)
    }
  },
  data() {
    return {
      searchLabel: '',
      hotLabels: [],
      isActive: -1
    }
  },
  created() {
    this.getHotLabel()
  },
  methods: {
    labelClick(label, index) {
      this.isActive = index
      this.$set(this, 'searchLabel', label)
    },
    getHotLabel() {
      getHotLabel().then(res => {
        this.hotLabels = (res.result || [])
      }).catch(err => {
        console.log(err)
      })
    },

    inputLabel(newValue) {
      console.log('inputLabel')
      let oldValue = null;
      this.$emit("update", { newValue, oldValue, field: this.field });
    }
  },
};
</script>

<style lang="scss" scoped>
.label-search {
  width: 100%;
}
.hot-labels {
  // width: calc(100% + 110px);
  width: 100%;
  // margin-left: -110px;
  margin-top: 12px;
  display: flex;
  flex-wrap: wrap;
  &-item {
    width: 136px;
    height: 28px;
    background: #FFFFFF;
    border-radius: 4px;
    border: 1px solid #D9D9D9;
    line-height: 28px;
    text-align: center;
    padding: 0 12px;
    overflow: hidden;
    text-overflow: ellipsis;
    white-space: nowrap;
    font-size: 14px;
    font-weight: 400;
    color: #262626;
    margin: 0 12px 12px 0;
    cursor: pointer;

    &:hover {
      background: $color-primary-light-1;
      border: 1px solid $color-primary-light-2;
      color: $color-primary-light-6;
    }
  }
  .is-active {
    background: #EDFBFB;
    border: 1px solid #D6F4F4;
    color: $color-primary-light-6;
  }
}
// .label-search-option {
//   display: flex;
//   justify-content: space-between;
// }
</style>